package org.wangp.gateway.oa.web.controller;

import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.KeyPair;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

/**
 * @author wangpeng (๑•ᴗ•๑)
 * @since 2021/10/6 3:54
 */
@RestController
public class KeyPairController {

	@Resource
	private KeyPair keyPair;

	@GetMapping("/rsa/publicKey")
	public Map<String, Object> getKey() {
		final RSAPublicKey publicKey = (RSAPublicKey) this.keyPair.getPublic();
		final RSAKey key = new RSAKey.Builder(publicKey).build();
		return new JWKSet(key).toJSONObject();
	}

}
